package com.iqismart.shorturl.model;

import com.iqismart.shorturl.vo.UserInfoResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import javax.persistence.*;

/**
 * @Description
 * @Auther sty
 * @createTime 2019-10-14 11:19
 */
@Entity
@Table(indexes = {@Index(name="usernameIndex", columnList="username"),
        @Index(name="openIdIndex", columnList="openId"),
        @Index(name="mobileIndex", columnList="mobile")} )
public class EntityUser {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator="userSeqGenerator")
    @SequenceGenerator(name = "userSeqGenerator", sequenceName = "user_sequence", initialValue = 10000, allocationSize = 1)
    private Long id;
    @Column(columnDefinition = "varchar(20)")
    private String username = "";
    @Column(columnDefinition = "varchar(20)")
    private String nickname = "";
    @Column(columnDefinition = "varchar(50)")
    private String password = "";
    @Column(columnDefinition = "varchar(20)")
    private String mobile = "";
    @Column(columnDefinition = "varchar(100)")
    private String openId = "";

    private Long createTime = System.currentTimeMillis();
    private Long updateTime = System.currentTimeMillis();

    public String getNickname() {
        if(StringUtils.isEmpty(nickname)){
            nickname = username;
        }
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Long createTime) {
        this.createTime = createTime;
    }

    public Long getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Long updateTime) {
        this.updateTime = updateTime;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getOpenId() {
        return openId;
    }

    public void setOpenId(String openId) {
        this.openId = openId;
    }

    public UserInfoResponse toResponse() {
        final UserInfoResponse response = new UserInfoResponse();

        BeanUtils.copyProperties(this,response);

        return response;
    }
}
